package com.hy.frame.util

import android.content.Context
import android.os.Build
import android.util.DisplayMetrics
import android.util.TypedValue
import android.view.View
import androidx.annotation.ColorRes

/**
 * title 常用的Dimension转换
 * author heyan
 * time 2018/4/6 11:14
 * desc 无
 */
object ResUtil {

    /**
     * px to dimension
     */
    fun px2Other(unit: Int, value: Float, metrics: DisplayMetrics): Float {
        return when (unit) {
            TypedValue.COMPLEX_UNIT_PX -> value
            TypedValue.COMPLEX_UNIT_DIP -> value / metrics.density
            TypedValue.COMPLEX_UNIT_SP -> value / metrics.scaledDensity
            TypedValue.COMPLEX_UNIT_PT -> value / (metrics.xdpi * (1.0f / 72))
            TypedValue.COMPLEX_UNIT_IN -> value / metrics.xdpi
            TypedValue.COMPLEX_UNIT_MM -> value / (metrics.xdpi * (1.0f / 25.4f))
            else -> 0f
        }
    }

    /**
     * dimension to px
     */
    fun other2px(unit: Int, value: Float, metrics: DisplayMetrics): Int {
        return Math.round(TypedValue.applyDimension(unit, value, metrics))
    }

    /**
     * px to sp
     */
    fun px2sp(context: Context, value: Float): Float {
        return px2Other(TypedValue.COMPLEX_UNIT_SP, value, context.resources.displayMetrics)
    }

    /**
     * px to dip
     */
    fun px2dip(context: Context, value: Float): Float {
        return px2Other(TypedValue.COMPLEX_UNIT_DIP, value, context.resources.displayMetrics)
    }

    /**
     * sp to px
     */
    fun sp2px(context: Context, value: Float): Int {
        return other2px(TypedValue.COMPLEX_UNIT_SP, value, context.resources.displayMetrics)
    }

    /**
     * dip to px
     */
    fun dip2px(context: Context, value: Float): Int {
        return other2px(TypedValue.COMPLEX_UNIT_DIP, value, context.resources.displayMetrics)
    }

    fun getColor(context: Context, @ColorRes resId: Int): Int {
        return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            context.resources.getColor(resId, context.theme)
        } else {
            context.resources.getColor(resId)
        }
    }

    /**
     * 获取 控件
     * @param id 行布局中某个组件的id
     * @param parent  parent
     */
    fun <T : View> findViewById(id: Int, parent: View?): T? {
        return parent?.findViewById(id)
    }

    /**
     * 获取控件的高度，如果获取的高度为0，则重新计算尺寸后再返回高度
     */
    fun getViewMeasuredHeight(view: View): Int {
        // int height = view.getMeasuredHeight();
        // if(0 < height){
        // return height;
        // }
        calcViewMeasure(view)
        return view.measuredHeight
    }

    /**
     * 获取控件的宽度，如果获取的宽度为0，则重新计算尺寸后再返回宽度
     */
    fun getViewMeasuredWidth(view: View): Int {
        // int width = view.getMeasuredWidth();
        // if(0 < width){
        // return width;
        // }
        calcViewMeasure(view)
        return view.measuredWidth
    }

    /**
     * 测量控件的尺寸
     */
    fun calcViewMeasure(view: View) {
        // int width = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);
        // int height = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);
        // view.measure(width,height);

        val width = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)
        val expandSpec = View.MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE shr 2, View.MeasureSpec.AT_MOST)
        view.measure(width, expandSpec)
    }
}